package cn.yx.common.web.configuration;

import cn.hutool.core.util.StrUtil;
import cn.yx.common.core.constant.SeparatorConstant;
import cn.yx.common.web.interceptor.LoggerInterceptor;
import cn.yx.common.web.properties.LoggerInterceptorProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * <p>日志拦截器配置类</p>
 *
 * @author Wgssmart
 */
@Configuration
@EnableConfigurationProperties(LoggerInterceptorProperties.class)
public class LoggerInterceptorConfiguration implements WebMvcConfigurer {

    private final LoggerInterceptorProperties loggerInterceptorProperties;

    public LoggerInterceptorConfiguration(LoggerInterceptorProperties loggerInterceptorProperties) {
        this.loggerInterceptorProperties = loggerInterceptorProperties;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // addPathPatterns拦截的路径
        String[] addPathPatterns = {};
        if (StrUtil.isNotEmpty(loggerInterceptorProperties.getAddPathPatterns())) {
            addPathPatterns = loggerInterceptorProperties.getAddPathPatterns().split(SeparatorConstant.COMMA);
        }
        // excludePathPatterns排除的路径
        String[] excludePathPatterns = {};
        if (StrUtil.isNotEmpty(loggerInterceptorProperties.getExcludePathPatterns())) {
            excludePathPatterns = loggerInterceptorProperties.getExcludePathPatterns().split(SeparatorConstant.COMMA);
        }
        registry.addInterceptor(new LoggerInterceptor()).addPathPatterns(addPathPatterns).excludePathPatterns(excludePathPatterns);
    }

}